package com.mobisys.live.controller;

import com.mobisys.live.bean.channel.CreateNewChannelBean;
import com.mobisys.live.bean.channel.GetChannelList;
import com.mobisys.live.dao.mapper.ChannelMapper;
import com.mobisys.live.dao.mapper.LiveMapper;
import com.mobisys.live.dao.mapper.PictureMapper;
import com.mobisys.live.dao.mapper.VideoMapper;
import com.mobisys.live.dao.model.Channel;
import com.mobisys.live.dao.model.Picture;
import com.mobisys.live.response.Response;
import com.mobisys.live.config.ResponseCode;
import com.mobisys.live.response.channel.ChannelResponseBody;
import com.mobisys.live.util.LiveUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by zhuo on 2018-01-16.
 * 频道相关的操作
 */

@RestController
public class ChannelController {

    @Autowired
    private ChannelMapper channelMapper;

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private PictureMapper pictureMapper;


    /**
     * 新建频道
     * @param createNewChannelBean
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/channel/createNewChannel",method = RequestMethod.POST)
    public Response createNewChannel(@Validated@RequestBody CreateNewChannelBean createNewChannelBean, BindingResult bindingResult,HttpServletRequest httpServletRequest){

        Response response=new Response();

        //检验输入的合法性
        if(bindingResult.hasErrors()){
            response.setRetureValue(INPUT_IS_INVALID);
            response.setRetureMsg(MSG_INPUT_IS_INVALID);

            return response;
        }

        //插入数据库
        Channel channel=new Channel();
        channel.setUserId(createNewChannelBean.getUserId());
        channel.setChannelName(createNewChannelBean.getChannelName());
        channel.setChannelNumber(LiveUtil.generateChannelNum()+"");
        channel.setCreateTime(new Date(System.currentTimeMillis()));
        channel.setInstruction("");

        int result=channelMapper.insertSelective(channel);
        if(result<=0){
            //插入失败
            response.setRetureValue(ResponseCode.DATABASE_ERROR);
            response.setRetureMsg(ResponseCode.MSG_DATABASE_ERROR);
            return response;
        }


        response.setRetureValue(SUCCESS);
        response.setRetureMsg(ResponseCode.MSG_SUCCESS);

        return response;

    }


    /**
     * 获取频道列表
     * @param getChannelList
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/channel/getChannelList",method = RequestMethod.POST)
    public Response getChannelList(@Validated@RequestBody GetChannelList getChannelList, BindingResult bindingResult,HttpServletRequest httpServletRequest){
        Response response=new Response<List<ChannelResponseBody>>();

        //检查输入的合法性
        if(bindingResult.hasErrors()){
            response.setRetureValue(INPUT_IS_INVALID);
            response.setRetureMsg(MSG_INPUT_IS_INVALID);

            return response;
        }

        //取出未被删除的频道列表
        List<Channel> channelList=channelMapper.selectByUserId(getChannelList.getUserId());
        List<ChannelResponseBody> channelResponseBodyList=new ArrayList<>();
        for (int i = 0; i <channelList.size() ; i++) {
            ChannelResponseBody channelResponseBody=new ChannelResponseBody();
            Channel channel=channelList.get(i);
            channelResponseBody.setChannelId(channel.getId());
            channelResponseBody.setChannelName(channel.getChannelName());
            channelResponseBody.setChannelNum(channel.getChannelNumber());

            //获取图片地址
            Picture picture=pictureMapper.selectByPrimaryKey(channel.getCoverPictureId());
            //channelResponseBody.setImgUrl(picture.getOriginal());
            channelResponseBody.setInstruction(channel.getInstruction());
            channelResponseBodyList.add(channelResponseBody);
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(channelResponseBodyList);

        //接口返回
        return response;

    }






}

